package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.Mapper.ZoneMapper;
import com.ruoyi.StarStudio.Domain.Annualoutput;
import com.ruoyi.StarStudio.Mapper.AnnualOutputMapper;
import com.ruoyi.StarStudio.Service.AnnualOutputService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

@Service
public class AnnualOutputServiceImpl implements AnnualOutputService {

    @Autowired
    private AnnualOutputMapper annualoutputMapper;

    @Autowired
    private ZoneMapper zoneMapper;

    @Autowired
    protected Validator validator;

    private static final Logger log = LoggerFactory.getLogger(AnnualOutputServiceImpl.class);

    //根据地块id查询年产量信息
    @Override
    public List<Annualoutput> getByZoneId(Integer zone_id) {
        return annualoutputMapper.getByZoneId(zone_id);
    }

    //根据年产量id查询年产量信息
    @Override
    public Annualoutput getByOutputId(Integer annualoutput_Id) {
        return annualoutputMapper.getByOutputId(annualoutput_Id);
    }

    //根据地块id增加年产量信息及中间表数据
    @Override
    public void add(Annualoutput annualoutput, Integer zone_id) {
        annualoutputMapper.add(annualoutput);  //增加信息至主表
        annualoutputMapper.addToZone_AnnualOutput(zone_id,annualoutput.getAnnualoutput_Id());  //增加信息至中间表
    }

    //修改年产量信息
    @Override
    public void update(Annualoutput annualoutput) {
        annualoutputMapper.update(annualoutput);
    }

    //根据年产量id删除年产量信息及中间表数据
    @Override
    public void delete(Integer annualoutput_Id) {
        annualoutputMapper.deleteFromZone_AnnualOutput(annualoutput_Id);  //删除中间表关联信息
        annualoutputMapper.delete(annualoutput_Id);  //删除主表信息
    }

    @Override
    public List<Annualoutput> getPageByZoneId(Integer zone_id) {
        return annualoutputMapper.getPageByZoneId(zone_id);
    }

    @Override
    public List<Annualoutput> getList(Annualoutput annualoutput) {
        return annualoutputMapper.getList(annualoutput);
    }

    @Override
    public String importAnnualOutput(List<Annualoutput> annualOutputList, boolean isUpdateSupport) {
        if (StringUtils.isNull(annualOutputList) || annualOutputList.size() == 0) {
            throw new ServiceException("导入年产量数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Annualoutput annualoutput : annualOutputList) {
            try {
                int zoneIdByZoneName = zoneMapper.getZoneIdByZoneName(annualoutput.getZone_name());
                annualoutput.setZone_id(zoneIdByZoneName);
                Annualoutput AOPByZoneAndYear = annualoutputMapper.getByZoneAndYear(annualoutput);
                if (StringUtils.isNull(AOPByZoneAndYear)) {
                    BeanValidators.validateWithException(validator, annualoutput);
                    annualoutputMapper.add(annualoutput);
                    //此时已获取到生成的id
                    annualoutputMapper.addToZone_AnnualOutput(zoneIdByZoneName,annualoutput.getAnnualoutput_Id());
                    successNum++;
                    successMsg.append("<br/>" + successNum + "."+ zoneMapper.getZoneNameByZoneId(annualoutput.getZone_id()) + "_" + annualoutput.getYear() + "年的年产量导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, annualoutput);
                    annualoutputMapper.update(annualoutput);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "."+ zoneMapper.getZoneNameByZoneId(annualoutput.getZone_id()) + "_" + annualoutput.getYear() + "年的年产量更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "."+ zoneMapper.getZoneNameByZoneId(annualoutput.getZone_id())+ "_" + annualoutput.getYear() + "年的年产量已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "."+ zoneMapper.getZoneNameByZoneId(annualoutput.getZone_id()) + "_" + annualoutput.getYear() + "年的年产量数据导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
